package com.demo.ctf;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SupplyAsyncDemo {
    public static void main(String[] args) throws Exception{
        /**
         * 创建一个线程池，该线程池使用提供的固定数量的线程来执行任务，最多5个线程。
         * 如果所有线程都在工作，那么新提交的任务将在队列中等待，直到有线程变得可用。
         */
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        /**supplyAsync 异步执行任务，任务有返回值*/
        CompletableFuture<String> task1 = CompletableFuture.supplyAsync(() ->{
            System.out.println(Thread.currentThread().getName());
            return "你好";
        },executorService);

        /**supplyAsync 异步执行任务，任务有返回值*/
        CompletableFuture<String> task2 = CompletableFuture.supplyAsync(() ->{
            System.out.println(Thread.currentThread().getName());
            return "你好1";
        });
        System.out.println(task1.get());
        System.out.println(task2.get());
    }

}